package com.etongwl.commonutils;

import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 常用的工具
 */
public class CommonUtils {
    /**
     * 正则表达式验证
     *
     * @param regEx
     * @param str
     * @return
     */
    public static boolean regExCheck(String regEx, String str) {
        Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * @throws
     * @Methods: 评分
     * @Description: 给我评分方法 跳到市场
     */
    public static void pingfen(Context con,String packageStr) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("market://details?id="+packageStr));
            con.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
           // MyToast.getInstance().showToast(con, "您尚未安装google play.");
        }
    }


    /**
     * @param mContext  上下文
     * @param className 判断的服务名字
     * @return true:在运行 false:不在运行
     * @throws
     * @Methods: isServiceRunning
     * @Description: 用来判断服务是否运行
     */
    public static boolean isServiceRunning(Context mContext, String className) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager
                .getRunningServices(30);
        if (!(serviceList.size() > 0)) {
            return false;
        }
        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className) == true) {
                isRunning = true;
                break;
            }
        }
        return isRunning;
    }

    /**
     * @param context
     * @throws
     * @Methods: clearNotificationManager
     * @Description: 清除通知
     */
    public static void clearNotificationManager(Context context) {
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.cancelAll();
        }
    }

    /**
     * Bitmap 转 byte
     * @param bitmap
     * @return
     */
    public static byte[] bitmaptoBytes(Bitmap bitmap) {
        // 将Bitmap转换成字符串
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        return bytes;
    }

}
